package com.taxshare.purchaser.service.rulesReview;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.taxshare.purchaser.constant.CodeConstant;
import com.taxshare.purchaser.entity.BlackWhiteEntity;
import com.taxshare.purchaser.entity.InvoiceCollectionEntity;
import com.taxshare.purchaser.entity.InvoiceWhiteRulesEntity;
import com.taxshare.purchaser.mapper.BlackWhiteMapper;
import com.taxshare.purchaser.mapper.InvoiceWhiteRulesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 白名单规则审核
 *
 * @author hjh
 */
@Component
public class WhiteReviewCheck implements RuleMainHandler {

    @Autowired
    BlackWhiteMapper blackWhiteMapper;

    @Autowired
    InvoiceWhiteRulesMapper whiteRulesMapper;

    /**
     * 白名单规则审核
     *
     * @param bean 发票实体
     * @param pid  审核规则id
     * @return
     */
    @Override
    public CheckInfoRespVO checkRule(InvoiceCollectionEntity bean, String pid) {
        InvoiceWhiteRulesEntity info = getInfo(pid);
        CheckInfoRespVO vo = new CheckInfoRespVO();
        if (info != null) {
            //黑白名单同一表，status 1->黑名单  0->白名单
            if (blackWhiteMapper.selectCount(new QueryWrapper<BlackWhiteEntity>()
                    .eq("tax_number", bean.getSellerTaxNo())
                    .eq("status", CodeConstant.WHITE_TYPE)) > 0) {
                vo.setGoing(false);
                vo.setFlag(true);
            }
        }
        return vo;
    }

    @Override
    public CheckInfoRespVO checkSubmit(String id) {
        return null;
    }

    /**
     * 查询规则开启状态
     *
     * @param pid
     * @return 白名单规则
     */
    private InvoiceWhiteRulesEntity getInfo(String pid) {
        InvoiceWhiteRulesEntity entity = whiteRulesMapper.selectOne(new QueryWrapper<InvoiceWhiteRulesEntity>().eq("uuid", pid));
        if (entity != null && CodeConstant.RULES_OPEN.equals(entity.getWhiteIsOpen())) {
            return entity;
        }
        return null;
    }
}


